package com.yy.controller.admin;

import com.yy.controller.service.AdminUserService;
import com.yy.dto.AdminUserDTO;
import com.yy.dto.AdminUserLoginDTO;
import com.yy.dto.AdminUserPageQueryDTO;
import com.yy.result.PageResult;
import com.yy.result.Result;
import com.yy.vo.AdminUserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
@Slf4j
@Api(tags = "后台管理系统账号相关接口")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    /**
     * 用户登录
     *
     * @param adminUserLoginDTO
     * @return
     */
    @PostMapping("/login")
    @CrossOrigin
    @ApiOperation(value = "用户登录")
    public Result<AdminUserLoginVO> login(@RequestBody AdminUserLoginDTO adminUserLoginDTO) {
        log.info("用户信息:{}", adminUserLoginDTO);
        AdminUserLoginVO adminUserLoginVO = adminUserService.login(adminUserLoginDTO);
        return Result.success(adminUserLoginVO);
    }

    /**
     * 分页查询用户列表
     *
     * @return
     */
    @GetMapping("/userList")
    @CrossOrigin
    @ApiOperation(value = "分页查询用户列表")
    public Result<PageResult> getUserPageList(AdminUserPageQueryDTO adminUserPageQueryDTO) {
        log.info("分页查询用户信息:{}", adminUserPageQueryDTO);
        PageResult pageResult = adminUserService.getUserPageList(adminUserPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增用户
     *
     * @return
     */
    @PostMapping("/addUser")
    @CrossOrigin
    @ApiOperation(value = "新增账号")
    public Result addUser(@RequestBody AdminUserDTO adminUserDTO) {
        log.info("新增账号:{}", adminUserDTO);
        adminUserService.addUser(adminUserDTO);
        return Result.success();
    }

    /**
     * 删除账号
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/{id}")
    @CrossOrigin
    @ApiOperation(value = "删除账号")
    public Result deleteUser(@PathVariable("id") Long id) {
        log.info("删除账号:{}", id);
        adminUserService.deleteUser(id);
        return Result.success();
    }

    /**
     * 修改状态
     *
     * @param id
     * @return
     */
    @PutMapping("/editStatus/{id}/{status}")
    @CrossOrigin
    @ApiOperation(value = "修改状态")
    public Result editStaus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("id:{},{}", id, status);
        adminUserService.editeStatus(id, status);
        return Result.success();
    }

    /**
     * 重置密码
     *
     * @param id * @return
     */
    @PutMapping("/resetPwd/{id}")
    @CrossOrigin
    @ApiOperation(value = "重置密码")
    public Result resetPwd(@PathVariable Long id) {
        log.info("id:{}", id);
        adminUserService.resetPwd(id);
        return Result.success();
    }

    /**
     * 根据账号名修改密码
     *
     * @param adminUserDTO
     * @return
     */
    @PostMapping("/updatePwd")
    @CrossOrigin
    @ApiOperation(value = "修改密码")
    public Result updatePassword(@RequestBody AdminUserDTO adminUserDTO) {
        log.info("修改密码:{}", adminUserDTO);
        adminUserService.updatePassword(adminUserDTO);
        return Result.success();
    }
}
